Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
How to test whether a string represents a "problematic" number?

I have a large array, named data, of string values from an external source, where each is supposed to represent an integer.

When I leave it to Javascript's automatic type conversion, there were no Javascript errors when I ran some arithmetic calculations on the whole array.

However, an operation like if (data[i] > data[i-1]) count++; gives the wrong result for some of the array entries. I don't know which.

When I added the following manual conversion step, then computation on the array of numbers gave the expected result:

data.forEach((n, i) => {
  data[i] = Number(n);
});

In other words, the same computation on data gives different outcomes when the above step is included or not.

As the array is large, it is not practical for me to comb through the data to pick out the offending ones. I want to find out where the problem is. I modified the forEach loop to:

data.forEach((n, i) => {
  if (data[i] != Number(n)) console.log("!!!", i, n);
  data[i] = Number(n);
});

but nothing was printed out.

What is a method to find out which values of the array are problematic?

data is like: [ "123", "234", "456", "789", .... ].


If you want to know how I knew there was a problem, a colleague used Python on the same set of data and produced a different result. That led to an investigation.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you're expecting valid integers in the strings, try this method to discover which ones might be problems:

const data = ["123", "234", "456", "789", "", "2,234", "2.02", "1e3", "n0", "invalid", "2", "10"];

for (const [index, str] of data.entries()) {
  if (parseInt(str, 10) !== Number(str)) console.log(index, str);
}

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda